package TestThread;

import java.util.Random;




public class demo7 {
    public static int sum1 = 0;
    public static int sum2 = 0;
    private static int MaxSize = 10000000;
    public static int[] array = new int[MaxSize];
    public static int[] ConstructArray(){
        Random random = new Random();
        for (int i = 0; i < MaxSize; i++) {
            int tmp = random.nextInt(100);
            array[i] = tmp;
        }
        return array;
    }
    public static void PrintArray(){
        for (int i = 0; i < MaxSize; i++) {
            System.out.println(array[i]);
        }
    }
    public static int GetSize(){
        return MaxSize;
    }
    public static void main(String[] args) throws InterruptedException {
        long starttime = System.currentTimeMillis();
        int[] array = demo7.ConstructArray();
        Thread t1 = new Thread(()->{

                for (int i = 0; i < demo7.GetSize(); i += 2) {
                    int id = i;
                    demo7.sum1 += array[id];
                }

        });
        Thread t2 = new Thread(()->{

            for (int i = 1; i < demo7.GetSize(); i += 2) {
                int id = i;
                demo7.sum2 += array[id];
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long endtime = System.currentTimeMillis();
        System.out.println(demo7.sum1+demo7.sum2);
        long time = endtime-starttime;
        System.out.println("执行时间 : "+ time);

    }
}
